/*
 * combat_insertion.sqf: This file is part of Combat Insertion
 * Version: Beta 0.815
 *
 * Copyright (C) 2010 Sacha Bartuweit 'Ir0n1E' <ir0n1e@freenet.de>
 *
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Discription:
 * null = [_group, _vehicle, _debug] execvm "combat_insertion.sqf"; 
 **/

_group      = _this select 0; 		/* Named group of units */ 
_vehicle    = _this select 1;		/* Named Vehicle/Chopper */
_debug      = _this select 2;		/* Debug true/false */
_cntgroup   = count units _group;
_emppos     = _vehicle emptypositions "cargo"; 
_count      = _emppos + _cntgroup;

/* No damage for all units.
 * The script is very trickie if we do not do this 
 */
{ _x allowDamage false } foreach units _group;
_vehicle allowDamage false;
 
{ _x allowDamage false } foreach crew _vehicle;


/* Main loop. */
while { _emppos != _count } do {
	_vehicle land "LAND";
	
	if ( _debug ) then {
		_vehicle sidechat format [".:: %1: Target: %2 Direction: %3 %4", 
                                            _vehicle, landResult _vehicle, getDir _vehicle, _height ]; 
	};
    
	_height = ( position _vehicle ) select 2;
	
	/* Drop units if height < 3.0. */
	if ( _height < 3.0 ) then { 
		{
			/* Disable animation and autotarget, so units get out faster. */
			_x DisableAI 'ANIM';
			_x disableAI "autoTarget";
			
            /* Disable firering. AI will shoot your units. */
			_vehicle setCombatMode "BLUE";
			
            [_x] allowGetIn false;
			_x setUnitPos "Middle";
			
			_x action ["getOut", _vehicle];
            	
			sleep 0.15;	
			
            /* Play the 'running move', so units do not jump on eachother. */
			// _x playMove "amovpercmevasraswrfldf";
			_x setUnitPos "AUTO";
			_x enableAI "autoTarget";
				
			_emppos = _vehicle emptypositions "cargo";		
            
			
			if ( _debug ) then {
                		_vehicle sidechat format [".:: %1: Height: %2 EmptyPos: %3 Count: %4 %5", 
                                            _vehicle, _height, _emppos, _count, _count - _emppos]; 
            	};
			 
        	} foreach units _group;
		
		    /* Disable landing and activate animations and damage. */
			_vehicle land "NONE";
			_vehicle allowDamage true;
	
		
		    sleep 0.5;
		    {_x EnableAI 'ANIM';_x allowDamage true; sleep 0.5} foreach units _group;
		    {_x allowDamage true } foreach crew _vehicle;

	};		
};

_vehicle setCombatMode "YELLOW";	

/* Exit the script. */
if (true) exitwith {
	if ( _debug ) then {
			hintSilent format [".:: %1: Script EXIT",  _vehicle];
	};
	if(RUM_THel_NonSpawn)then{
		_vehicle setVariable ["RUM_THel_continue",true,true];
	}else{
		_stg = format ["%1",_vehicle];
		RUM_THel_continue = _stg;
	};
};

/* vi:ts=4:sw=4:wrap:ai:expandtab */
